package com.qs.eggs.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.bean.OrderBean;
import com.qs.eggs.bean.SortBean;
import com.qs.eggs.refresh.adapter.BaseRecyclerAdapter;
import com.qs.eggs.utils.SpacesItemDecoration;
import com.qs.eggs.view.activity.EvaluationActivity;
import com.qs.eggs.view.activity.PayMethodActivity;
import com.qs.eggs.view.ordermanager.activity.WaitEvaluationActivity;
import com.qs.eggs.view.ordermanager.activity.WaitPayActivity;
import com.qs.eggs.view.prodetail.activity.ProDetailActivity;
import com.qs.eggs.widgets.custom.CustomerListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


public class OrderManagerAdapter extends BaseRecyclerAdapter<OrderBean> implements BaseRecyclerAdapter.OnLoadingHeaderCallBack {
private String id;
private List<Map<String, Object>>mlist = null;
    private PicAdapter picAdapter;
    public OrderManagerAdapter(Context context, int mode) {
        super(context, mode);

        setOnLoadingHeaderCallBack(this);
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderHolder(ViewGroup parent) {
        return new HeaderViewHolder(mHeaderView);
    }

    @Override
    public void onBindHeaderHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        return new SortViewHolder(mInflater.inflate(R.layout.item_order, parent, false));
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, final OrderBean item, int position) {
        final SortViewHolder vh = (SortViewHolder) holder;
        vh.mid.setText(item.getId());
        vh.mOrderNumber.setText("订单号: "+item.getOrderno());
       // vh.mContent.setText(item.getNote());
//        vh.mPrice.setText(""+item.getTotalmoney());
//        vh.mCount.setText("X "+item.getTotalcount());
        vh.mAllMoney.setText("共"+item.getTotalcount()+"件商品、合计："+item.getTotalmoney());
        vh.mCreateTime.setText(item.getCreatetime());
        //vh.mAvatar.setImageURI(Constants.URL_BASE+"productimages/"+item.getAutoname());


        HashMap<String,String> map=new HashMap<>();
        map.put("orderid",vh.mid.getText().toString());
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=searchMyOrderDetail")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("订单详情=="+s);
                        try {
                            mlist=new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject=jsonArray.getJSONObject(i);
                                Map<String,Object> map=new HashMap<>();
                                map.put("proid",jsonObject.getString("proid"));
                                map.put("price",jsonObject.getString("price"));
                                map.put("type",jsonObject.getString("type"));
                                map.put("proname",jsonObject.getString("proname"));
                                map.put("autoname",jsonObject.getString("autoname"));
                                map.put("count",jsonObject.getString("count"));
                                map.put("orderid",jsonObject.getString("orderid"));
                                mlist.add(map);
                            }
                            picAdapter = new PicAdapter(mContext, mlist);
                            vh.mListView.setAdapter(picAdapter);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
        if(item.getOrderstatus().equals("0")){
            vh.mOrderState.setText("待付款");
            vh.mHandler.setText("立即付款");
            vh.mdelete.setVisibility(View.VISIBLE);
            vh.mHandler.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mContext, PayMethodActivity.class);
                    intent.putExtra("orderno",item.getOrderno());
                    intent.putExtra("ordermoney",item.getTotalmoney());
                    intent.putExtra("biaoshi","2");
                    mContext.startActivity(intent);
                }
            });
            vh.mdelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delete(item.getId());
                }
            });
        }else if (item.getOrderstatus().equals("1")){
            vh.mOrderState.setText("待发货");
            vh.mHandler.setVisibility(View.GONE);
            vh.mdelete.setVisibility(View.GONE);
        }else if (item.getOrderstatus().equals("2")){
            vh.mOrderState.setText("待收货");
            vh.mHandler.setText("确认收货");
            vh.mdelete.setVisibility(View.GONE);
            vh.mHandler.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    confirmOrder(item.getId());
                }
            });
        }else if (item.getOrderstatus().equals("3")){
            vh.mOrderState.setText("待评价");
            vh.mHandler.setText("去评价");
            vh.mdelete.setVisibility(View.GONE);
            vh.mHandler.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mContext, EvaluationActivity.class);
                    intent.putExtra("orderid",item.getId());
                    mContext.startActivity(intent);
                }
            });
        }else if (item.getOrderstatus().equals("4")){
            vh.mOrderState.setText("订单完成");
            vh.mHandler.setVisibility(View.GONE);

        }else {

        }

    }
    //删除未付款的订单
    private void delete(String id){
        HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=deleteOrder")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if(s.equals("\"true\"")){
                            if (WaitPayActivity.instance!=null) {
                                WaitPayActivity.instance.finish();
                            }
                            Intent intent=new Intent(mContext,WaitPayActivity.class);
                            mContext.startActivity(intent);
                        }else {
                            Toast.makeText(mContext,"删除失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
}
    //确认收货
    private void confirmOrder(String id){
        final HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=confirmOrder")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("确认收货=="+s);
                        if (s.equals("\"true\"")){
                        Toast.makeText(mContext,"确认收货成功",Toast.LENGTH_SHORT).show();
                          Intent intent=new Intent(mContext, WaitEvaluationActivity.class);
                          mContext.startActivity(intent);
        }
                    }
                });
    }

    private static class SortViewHolder extends RecyclerView.ViewHolder {
        TextView mOrderNumber,mOrderState,mContent,mPrice,mCount,mAllMoney,mCreateTime,mHandler,mid,mdelete;
        CustomerListView mListView;



        public SortViewHolder(View itemView) {
            super(itemView);

            mOrderNumber=(TextView) itemView.findViewById(R.id.tv_order_number);
            mOrderState=(TextView) itemView.findViewById(R.id.tv_order_state);
//            mContent=(TextView) itemView.findViewById(R.id.tv_content);
//            mPrice=(TextView) itemView.findViewById(R.id.tv_price);
//            mCount=(TextView) itemView.findViewById(R.id.tv_count);
            mAllMoney=(TextView) itemView.findViewById(R.id.tv_allmoney);
            mCreateTime=(TextView) itemView.findViewById(R.id.tv_createtime);
            mHandler=(TextView) itemView.findViewById(R.id.tv_hander);
            mListView=(CustomerListView)itemView.findViewById(R.id.listView);
            mid=(TextView)itemView.findViewById(R.id.id);
            mdelete=(TextView)itemView.findViewById(R.id.delete);
            //mAvatar=(SimpleDraweeView) itemView.findViewById(R.id.propic);
        }
    }

}
